package com.mxnet.chongjin.supoman.activity;

import android.app.Instrumentation;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.adapter.LvAllMachineAdapter;
import com.mxnet.chongjin.supoman.bean.HotMachine;
import com.mxnet.chongjin.supoman.config.Api;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class AllMachineActivity extends AppCompatActivity implements View.OnClickListener {
    private Context mContext;
    private LinearLayout mLinearLayout;
    private ListView mListView;
    private LvAllMachineAdapter mAdapter;
    private List<HotMachine> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_machine);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        mContext = AllMachineActivity.this;

        initView();
        initData();
    }

    public void initData() {
        mList = new ArrayList();
        //        AllMachine machine1 = new AllMachine(R.mipmap.all_pic_machine_1, "Robotic Lawn Mower YZ-2",
        //                R.mipmap.all_pic_machine_2, "ROBOTIC LAWN MO-WER CC150");
        //        mList.add(machine1);
        //        AllMachine machine2 = new AllMachine(R.mipmap.all_pic_machine_3, "ROBOTIC LAWN MO-WER YZ-1",
        //                R.mipmap.all_pic_machine_4, "ROBOTIC LAWN MO-WER SPM14-320");
        //        mList.add(machine2);
        //        AllMachine machine3 = new AllMachine(R.mipmap.all_pic_machine_5, "ROBOTIC LAWN MO-WER SPM13-320",
        //                R.mipmap.all_pic_machine_6, "ROBOTIC LAWN MO-WER SPM08-320");
        //        mList.add(machine3);

        OkHttpUtils.get()
                .url(Api.URL_HEADER + Api.ALL_MACHINE)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        if (!TextUtils.isEmpty(response)) {
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                JSONArray data = jsonObject.optJSONArray("data");
                                for (int i = 0; i < data.length(); i++) {
                                    HotMachine hotMachine = new HotMachine();
                                    JSONObject object = data.optJSONObject(i);
                                    hotMachine.setId(object.optString("id"));
                                    hotMachine.setRobotImgUrl(object.optJSONObject("imgResource").optString("url"));
                                    hotMachine.setRobotNo(object.optString("robotNo"));
                                    mList.add(hotMachine);
                                    mAdapter = new LvAllMachineAdapter(mContext, mList);
                                    mListView.setAdapter(mAdapter);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    public void initView() {
        mLinearLayout = findViewById(R.id.ll_all_machine);
        mLinearLayout.setOnClickListener(this);

        mListView = findViewById(R.id.lv_all_machine);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_all_machine:
                new Thread() {
                    public void run() {
                        try {
                            Instrumentation inst = new Instrumentation();
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
        }
    }
}
